package net.zdsoft.wpchat.support.action.auth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.zdsoft.keel.action.ActionContext;
import net.zdsoft.keel.util.Validators;
import net.zdsoft.wpchat.android.client.BasicAction;
import net.zdsoft.wpchat.support.entity.AuthCatalog;
import net.zdsoft.wpchat.support.entity.AuthConfig;
import net.zdsoft.wpchat.support.entity.AuthGroup;
import net.zdsoft.wpchat.support.entity.GroupPermission;
import net.zdsoft.wpchat.support.service.AuthService;

/**
 * 模块授权部分
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2014-9-22 下午8:58:50 $
 */
public class AuthPermissionAction extends BasicAction {
    private static final long serialVersionUID = -5610995384706471559L;

    @Resource
    private AuthService authService;

    private List<AuthGroup> groupList = new ArrayList<AuthGroup>();
    private String groupId;
    private String[] operationId;
    private AuthConfig authConfig;
    private String catalogId;
    private List<AuthCatalog> catalogList = new ArrayList<AuthCatalog>();
    private Map<String, GroupPermission> opId2GroupPermMap = new HashMap<String, GroupPermission>();

    /**
     * 显示权限列表
     * 
     * @return
     */
    public String listPermission() {
        groupList = authService.getAllGroup(getSupportMemoryUser().getId());
        authConfig = authService.getAuthConfig(getSupportMemoryUser().getId());
        if (Validators.isEmpty(groupId)) {
            if (!groupList.isEmpty()) {
                groupId = groupList.get(0).getId();
            }
            else {
                addActionError("目前组还不存在,请先增加组");
                return SUCCESS;
            }
            catalogId = "";
        }

        if (Validators.isEmpty(catalogId)) {
            catalogList = authConfig.getCatalogs();
        }
        else {
            catalogList.add(authConfig.getCatalog(catalogId));
        }
        opId2GroupPermMap = authService.getOperationId2GroupPermMap(groupId);
        return SUCCESS;
    }

    /**
     * 添加权限
     * 
     * @return
     */
    public String addPermission() {
        if (Validators.isEmpty(groupId)) {
            addActionError("请选择组");
            groupList = authService.getAllGroup(getSupportMemoryUser().getId());
            authConfig = authService.getAuthConfig(getSupportMemoryUser().getId());
            return INPUT;
        }
        opId2GroupPermMap = new HashMap<String, GroupPermission>();
        HttpServletRequest request = ActionContext.getRequest();

        if (Validators.isEmpty(operationId)) {
            operationId = new String[0];
        }

        for (int i = 0; i < operationId.length; i++) {
            GroupPermission perm = new GroupPermission();
            perm.setAuthOperationId(operationId[i]);
            perm.setGroupId(groupId);

            String permision = request.getParameter(operationId[i] + "_Permision");
            if (Validators.isEmpty(permision)) {
                addActionError("请选择所有项的操作类型");
                groupList = authService.getAllGroup(getSupportMemoryUser().getId());
                authConfig = authService.getAuthConfig(getSupportMemoryUser().getId());
                return INPUT;
            }
            perm.setPermission(Integer.parseInt(permision));

            opId2GroupPermMap.put(perm.getAuthOperationId(), perm);
        }

        // 增加组权限
        List<GroupPermission> _permList = new ArrayList<GroupPermission>();
        _permList.addAll(opId2GroupPermMap.values());
        authService.modifyGroupPerms(groupId, catalogId, _permList.toArray(new GroupPermission[0]));

        return SUCCESS;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public List<AuthGroup> getGroupList() {
        return groupList;
    }

    public AuthConfig getAuthConfig() {
        return authConfig;
    }

    public Map<String, GroupPermission> getOpId2GroupPermMap() {
        return opId2GroupPermMap;
    }

    public String getCatalogId() {
        return catalogId;
    }

    public void setCatalogId(String catalogId) {
        this.catalogId = catalogId;
    }

    public List<AuthCatalog> getCatalogList() {
        return catalogList;
    }

    public String[] getOperationId() {
        return operationId;
    }

    public void setOperationId(String[] operationId) {
        this.operationId = operationId;
    }

}
